<template>
	<view class="patent_detail_wrap">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">商品列表</block>
		</cu-custom>
        <view class='back' @click="back"></view>
		<view class="list-item" v-for='(item,index) in rankingNew' :key=index>
			<view class="content">
				<text class='title'>商品名称：{{item.title}}</text>
				<text class='inventor'>咨询人：{{item.inventor}}</text>
			</view>
			<view class="action">
				<button class="cu-btn round shadow" :class="item.member_id == userData.member_id ? 'bg-red': 'bg-blue'" style="position: absolute;right:20px;top:20px" @click="CreateChat(item.member_id)">点击咨询
                    {{item.member_id == userData.member_id ? '  自己' :item.member_id}}
                    </button>
			</view>
		</view>
        <button class="cu-btn round bg-blue bye"  @click="Bye">退出登录</button>
		<newMessage ref='newMessage'></newMessage>
        <button class="go cu-btn round bg-blue"  @click="goNewList">前往会话列表页</button>
	</view>
</template>

<script>
	import config from "../../API/config";
	import uniPopup from "@/components/uni-popup/uni-popup.vue";
	import uniPopupMessage from "@/components/uni-popup/uni-popup-message.vue";
	import uniPopupDialog from "@/components/uni-popup/uni-popup-dialog.vue";
	import all from "../../API/so.js";
	import newMessage from '../../asstess/newMessage/newMessage'
	export default {
		data() {
			return {
				socketInstance: null,
				userInfo: null,
				rankingNew: [],
				rankingHot: [],
				rankingCollect: [],
				initGoods: [],
				goods: [],
				initList: "",
				p: 1,
				total: "",
				list: [],
				money: 1000,
				chatContent: "asdfasdf",
				params: {
					key: "",
					loginKey: "",
					token: "",
					sertype: 0,
					isForceLogin: null,
                },
                userData:[],
                memberIds:[],
                loginData:[]
			};
		},
		methods: {
            Bye(){
            localStorage.clear()
            sessionStorage.clear();
            uni.navigateTo({
                    url:'/pages/login/login'
            })
            window.location.reload()
            },
            back() {
            uni.redirectTo({
                url: "/"
            });
            },
            goNewList(){
                uni.switchTab({
                    url: "../talk/verticalnav"
                }); 
            },
			// 初始化
			getInit() {
				this.$http({
					url: config.get("app_url") + `/index`,
					data: {
					},
					success: (res) => {
                        if(res.code == 200){
                            let needData = []
                            this.initList = res.data;
                            this.rankingHot = res.data.ranking.host;
                            needData = res.data.ranking.new;
                            this.rankingCollect = res.data.ranking.collectionNum;
                            this.initGoods = res.data.goods;
                            this.goods = res.data.goods.clickNum.slice(0, 6);
                            if(this.memberIds){
                                needData.map((item, index) => {
                                    item.member_id = ''
                                    item.member_id = this.memberIds[index]
                                })
                                this.rankingNew = needData
                            }
                        }
						
					},
				});
			},
			// 获取首页列表数据
			getList() {
				this.$http({
					url: config.get("app_url") + `/goodsList`,
					data: {
						p: this.p,
					},
					success: (res) => {
						this.total = res.data.count;
						this.list = res.data._list;
					},
				});
			},
			// 创建临时会话
			CreateChat(member_id) {
                console.log(member_id)
                let title = "一起聊天吧！";
                let params = {
                    title:title,
                    toMemberId: member_id,
                    content: {
                        title: "一起聊天吧！",
                        pic: "avatar.jpg",
                        url: "www.ba",
                    }
                }
                this.$Socket.action('makeDialogue',params,
                (talk_id)=>{
                    console.log(talk_id,'创建临时会话',params)
                    this.$Socket.action('getTalkList',null,(res)=>{
                        if(res.code == 200){
                            uni.navigateTo({
                                url: `/pages/talk/chat?id=`+talk_id.body.data.talk.id,
                            });
                        }
                    },()=>{})
                },
                (err)=>{
                    console.log('出错:'+err)
                })
            },
            openData () {
                let newChatAlert = (data) =>{
                    console.log('zhuanli页第1回调',data)
                    if (data.head.fromMemberId != this.userData.member_id) {
                        console.log('data新消息',data);
                        this.$refs.newMessage.showModal(data,this.userData.member_id) 
                    }
                }
                let listRefresh = (data) =>{
                    console.log(data,'zhuanli页第2回调')
                    if(data != undefined){
                        this.$store.commit('setGetTalkList',data)
                        console.log('',this.$store.state.getTalkList)
                    }
                }
                let talkDetailRefresh = (data) =>{
                    console.log(data,'zhuanli页第3回调')
                }
                let talkWithMsg = (msg) => {
                    console.log('zhuanli页第4回调',msg)
                }
                this.$Socket.action("addTalkObjectCallback", {
                    onNewChatAlert:newChatAlert,
                    onListRefresh:listRefresh,
                    onTalkDetailRefresh:talkDetailRefresh,
                    onTalkWithMsg:talkWithMsg,
                },()=>{},()=>{});
            }
		},
		onLoad() {
            let userData  = sessionStorage.getItem('userData')
		    let memberIds  = sessionStorage.getItem('memberIds')
            let loginData = sessionStorage.getItem('loginDatas')
            this.userData = JSON.parse(userData)
		    this.memberIds = JSON.parse(memberIds)
            this.loginData = JSON.parse(loginData)
			this.getInit();
            // 重新登录
            // this.$Socket.action('quickConnect',{
            //     key:this.loginData.key,
            //     token:this.loginData.token,
            //     member_id:this.userData.member_id,
            //     port:this.userData.port,
            // },()=>{},()=>{})
            this.openData()
            // 获取本地存储内数据
		    
		},
		components: {
			uniPopup,
			uniPopupMessage,
			uniPopupDialog,
			newMessage
		},
		watch: {
			chatContent: function() {},
		},
	};
</script>

<style lang="less">
    .go{
        width:100%;
        height:50px;
        position: fixed;
        top: 336px;
    }
    .back{
        background-color: transparent;
        width: 62px;
        height: 45px;
        position: fixed;
        top: 0px;
        z-index: 9999;
    }
    .bye{
        position: fixed;
        top:100px
    }
	.list-item {
		width: 100%;
		height: 50px;
		padding: 15px;
		position: relative;
		margin-bottom: 20px;

  .content {
    width: 80%;
    position: absolute;
    height: 50px;
    .title {
      font-size: 26rpx;
      font-weight: 700;
      position: absolute;
      top: 0px;
      left: 0;
      display: inline-block;
      width: 80%;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .inventor {
      float: left;
      margin-left: 0px;
      margin-top: 8px;
      position: absolute;
      top: 20px;
      left: 0;
      width: 80%;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

ul {
  padding: 0;

  li {
    height: 80rpx;
    list-style: none;
    display: flex;
    justify-content: space-between;

    .chat {
      color: blue;
    }
  }
}
</style>
